<?php
/*
 * Constructs a bargraph given the data passed to it
 *
 * Parameters:
 * $ydata -> an array which contains all values needed to be graphed.
 * $title -> a string which indicates the title which would identify the graph.
 * $height -> an integer which indicates how big will the graph be.
 *
 * Return Value:
 * $graph -> the graph created by this function.
 */
function barchart($ydata, $title, $height)
{
	//loads the libraries to be used
	require_once ('jpgraph/jpgraph.php');
	require_once ('jpgraph/jpgraph_bar.php');
	
	//constructs the graph given the specified dimensions
	$graph = new Graph(800, $height, "auto");
	$graph->SetScale('textlin');
	
	//adjusts the margin should the title take multiple lines
	if (strlen($title) > 80)
	{
		$graph->Set90AndMargin(0,0,70,0);
	}else
	{
		$graph->Set90AndMargin(0,0,40,0);
	}
	
	//separates the title if it is too long
	$temp = $title;
	$temp2 = "";
	while (strlen($temp) > 80)
	{
		$temp2 = $temp2 . substr($temp, 0, 80) . "\n";
		$temp = substr($temp, 80);
	}
	$title = $temp2 . $temp;
	
	//define some attributes for the graph
	$graph->title->Set($title);
	$graph->title->SetFont(FF_DEFAULT, FS_BOLD, 10);
    $graph->title->SetColor('black');
	$graph->xaxis->title->SetAngle(90);
	
	//construct the bargraph given the data
	$barplot=new BarPlot($ydata);
	$barplot->SetColor('blue');

	//add the plot to the graph
	$graph->Add($barplot);

	//return the graph
	return $graph;
	
}

/*
 * Constructs a piegraph given the data passed to it
 *
 * Parameters:
 * $ydata -> an array which contains all values needed to be graphed.
 * $title -> a string which indicates the title which would identify the graph.
 * $labels -> an array containing the legends for the values.
 *
 * Return Value:
 * $graph -> the graph created by this function.
 */
function piechart($ydata, $title, $labels)
{
	//loads the libraries to be used
	require_once ('jpgraph/jpgraph.php');
	require_once ('jpgraph/jpgraph_pie.php');
	require_once ('jpgraph/jpgraph_pie3d.php');
	
	//construct the pie graph given the predefined dimensions
	$graph = new PieGraph(800,400, 'auto');

	//define some attributes of the graph
	$theme_class= new VividTheme;
	$graph->SetTheme($theme_class);
		
	//construct the pie graph given the data	
	$pie = new PiePlot3D($ydata);

	//separates the title if it is too long
	$temp = $title;
	$temp2 = "";
	while (strlen($temp) > 80)
	{
		$temp2 = $temp2 . substr($temp, 0, 80) . "\n";
		$temp = substr($temp, 80);
	}
	$title = $temp2 . $temp;
	
	//define some more attributes of the pie graph
	$pie->title->Set($title);
	$pie->title->SetFont(FF_DEFAULT, FS_BOLD, 10);
    $pie->title->SetColor('black');
	$pie->ShowBorder();
	$pie->SetColor('black');
	$pie->ExplodeSlice(1);

	//set the legends that correspond to the data
	$pie->SetLegends($labels);	
	$pie->SetLabelPos(0.5);
	
	//add the plot to the graph
	$graph->Add($pie);
	
	//return the graph
	return $graph;
}

/*
 * Combines multiple graphs into a single one
 * 
 * Parameters:
 * $graphs -> an array containing all the graphs to be combined.
 * $height -> an array containing all the height for each graph to be combined.
 *
 * Return Value:
 * $mgraph -> the combined graph produced
 */
function combined_graph($graphs, $height)
{
	//load the library to be used
	require_once ('jpgraph/jpgraph_mgraph.php');
	
	//initialization of values
	$N = count($graphs);
	$mgraph = new MGraph();
	$mgraph->SetMargin(10,10,5,5);
	$h = $height[0];
	
	//add the each graphs to be combined indicated by a corresponding x and y position
	for($i = 0; $i<$N; $i++)
	{
		if($i==0)
		{
			$xpos1 = 10; $ypos1 = 5;
		}else{
			
			$xpos1 = 10; $ypos1 = $h +5;
			$h = $h + $height[$i];
		}
		$mgraph->Add($graphs[$i],$xpos1,$ypos1);
	}
	
	//return the combined graph
	return $mgraph;
}
?>